﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class GiftSetForm : Form
    {
        string id = "0";
        public GiftSetForm(string ids)
        {
            InitializeComponent();
            id = ids;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"]; 
        }

        private void GiftSetForm_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;
            this.timer1.Start();
            if (!id.Equals("0"))
            {
                gifts git = new gifts();
                Model.gifts gifMod = new Model.gifts();
                gifMod = git.GetModel(int.Parse(id));
                this.TxtGiftName.Text = gifMod.giftName;
                this.TxtInteral.Text = gifMod.Integral.ToString();
                this.TxtOther.Text = gifMod.memo;
                this.TxtAddAmount.Text = gifMod.giftamount.ToString();
                this.Text = "修改礼品";
            }
        }


        private void isNull()
        {
            string message = "";
            if (string.IsNullOrEmpty(TxtGiftName.Text) || string.IsNullOrEmpty(TxtInteral.Text) || string.IsNullOrEmpty(TxtAddAmount.Text))
            {
                message += "请输入";
            }
            if (string.IsNullOrEmpty(TxtGiftName.Text))
            {
                message += "礼品名称，";
            }
            if (string.IsNullOrEmpty(TxtInteral.Text))
            {
                message += "所需积分，";
            }
            if (string.IsNullOrEmpty(TxtAddAmount.Text))
            {
                message += "兑换个数，";
            }

            if (message != "")
            {
                message = message.Substring(0, message.Length - 1);
                MessageBox.Show(message);
                return;
            }
        }

        private void btnDetermine_Click(object sender, EventArgs e)
        {
            
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void GiftSetForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDetermine_Click_1(object sender, EventArgs e)
        {
            gifts gif = new gifts();
            Model.gifts gifMod = new Model.gifts();
            if (this.Text == "新增礼品")
            {
                isNull();
                gifMod.giftName = TxtGiftName.Text;
                gifMod.Integral = int.Parse(TxtInteral.Text);
                gifMod.giftamount = int.Parse(TxtAddAmount.Text);
                gifMod.memo = TxtOther.Text;
                int a = gif.Add(gifMod);
                if (a > 0)
                {
                    MessageBox.Show("添加新礼品成功！");
                }
                else
                {
                    MessageBox.Show("添加新礼品失败！");
                }

            }
            if (this.Text == "修改礼品")
            {
                gifMod = gif.GetModel(int.Parse(id));
                gifMod.giftName = TxtGiftName.Text;
                gifMod.Integral = int.Parse(TxtInteral.Text);
                gifMod.memo = TxtOther.Text;
                gifMod.giftamount = int.Parse(TxtAddAmount.Text);
                bool a = gif.Update(gifMod);
                if (a)
                {
                    MessageBox.Show("修改礼品成功！");
                }
                else
                {
                    MessageBox.Show("修改礼品失败！");
                }

            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            gifts gif = new gifts();
            Model.gifts gifMod = new Model.gifts();
            if (this.Text == "新增礼品")
            {
                isNull();
                gifMod.giftName = TxtGiftName.Text;
                gifMod.Integral = int.Parse(TxtInteral.Text);
                gifMod.giftamount = int.Parse(TxtAddAmount.Text);
                gifMod.memo = TxtOther.Text;
                int a = gif.Add(gifMod);
                if (a > 0)
                {
                    MessageBox.Show("添加新礼品成功！");
                }
                else
                {
                    MessageBox.Show("添加新礼品失败！");
                }

            }
            if (this.Text == "修改礼品")
            {
                gifMod = gif.GetModel(int.Parse(id));
                gifMod.giftName = TxtGiftName.Text;
                gifMod.Integral = int.Parse(TxtInteral.Text);
                gifMod.memo = TxtOther.Text;
                gifMod.giftamount = int.Parse(TxtAddAmount.Text);
                bool a = gif.Update(gifMod);
                if (a)
                {
                    MessageBox.Show("修改礼品成功！");
                }
                else
                {
                    MessageBox.Show("修改礼品失败！");
                }

            }
            this.Close();
        }

        private void TxtInteral_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        private void TxtAddAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }
    }
}
